home *** CD-ROM | disk | FTP | other *** search
- Path: in1.uu.net!pipeline!not-for-mail
- From: dkolmar@grovestocktn.com (Doug Kolmar)
- Newsgroups: comp.lang.c
- Subject: Fortran to C conversion help?
- Date: 4 Mar 1996 09:53:08 -0500
- Organization: The Pipeline
- Message-ID: <4hf04k$ogs@pipe3.nyc.pipeline.com>
- NNTP-Posting-Host: pipe3.nyc.pipeline.com
- X-PipeUser: dkolmar
- X-PipeHub: nyc.pipeline.com
- X-PipeGCOS: (Doug Kolmar)
- X-Newsreader: The Pipeline v3.3.0
-
-
- Hello folks,
-
- I'm sort of a weekend C programmer so perhaps I've missed something, but
- I'm trying to convert a function in FORTRAN to C.
-
- Attached is the FORTRAN code followed by the C code that I've written. The
- C program will compile and run, but does not yield the correct output. I.E.
- integers in the range 0 to 31.
-
- Except for the differences in the range of the RAND functions, I know
- nothing about FORTRAN, so am I missing something implicit in the code? Any
- help would be greatly appreciated.
-
- Doug Kolmar
- dkmar@panix.com
-
- FORTRAN:
-
- FUNCTION I1OVRF(LAST)
- C THE ARGUMENT, LAST, IS THE PREVIOUS VALUE OF THE SEQUENCE
- NEW=0
- C THE VARIABLE K EQUALS ONE-HALF THE NUMBER OF POSSIBLE VALUES
- K=16
- L=LAST
- C THE VARIABLE PROBIT EQUALS 1/(NUMBER OF POSSIBLE VALUES)
- PROBIT = .03125
- J=L/K
- IF (J.EQ.1) L=L-K
- U=RAND(0)
- IF (U.LT.PROBIT) J=1-J
- NEW = NEW+J*K
- K=K/2
- PROBIT=PROBIT*2
- IF (K.GE.1) GO TO 20
- I1OVRF=NEW
- RETURN
- END
-
-
- -------------------------------------------------------------------
- C:
-
- #include<stdlib.h>
- #include<time.h>
-
- int rand();
- void srand(unsigned seed);
- time_t time(time_t *tmhold);
-
- main()
- {
- int l,last,j,i;
- float probit, u, k, new;
- unsigned seed;
- time_t *tmhold;
-
- last = 30 (could be any value 0-31);
- for(i=0;i < 20;++i)
- {
- last = new;
- new=0;
- k=16;
- l=last;
- probit = .03125;
-
- while(k >= 1)
- {
- j = ceil(l/k);
- if(j=1) l=l-k;
- seed=time(tmhold);
- srand(seed);
- u = rand();
- u =1/u;
- if(u < probit) j = 1-j;
- new = new+j*k;
- k = k/2;
- probit = probit * 2;
- }
- printf("new= %f \n ",new);
- }
- }
-
-
-
-
-